package com.park.controller;

import com.park.bean.Tickets;
import com.park.bean.TicketsRec;
import com.park.bean.TicketsType;
import com.park.common.CommonResult;
import com.park.service.ITicketsRecService;
import com.park.util.Page;
import org.apache.poi.hpsf.Decimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class TicketsRecController {
    @Autowired
    ITicketsRecService ticketsRecService;

    @GetMapping("trec")
    public CommonResult getTicketsRec(TicketsRec ticketsRec, Page page){
        System.out.println(ticketsRec.getRecdate()+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        List<TicketsRec> res=ticketsRecService.getTicketsRec(ticketsRec,page);
        int count=ticketsRecService.getCount(ticketsRec);
        HashMap map=new HashMap();
        map.put("res",res);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @GetMapping("trec/tickets")
    public CommonResult getTicketsFromRec(){
        List<Tickets> list=ticketsRecService.getTicketsFromRec();
        return CommonResult.success(list);
    }
    @GetMapping("trec/type")
    public CommonResult getTicketsTypeFromRec(){
        List<TicketsType> list=ticketsRecService.getTicketsTypeFromRec();
        return CommonResult.success(list);
    }

    //添加数据
    @PostMapping("trec")
    public CommonResult postTicketsRec(@RequestBody TicketsRec ticketsRec){
        ticketsRec.setRecdate(LocalDate.now());
        int count=ticketsRecService.checkExist(ticketsRec);
        System.out.println(ticketsRec.getTicketsId());
        System.out.println(ticketsRec.getTypeId());
        System.out.println(ticketsRec.getRecdate());
        if(count==1){
            System.out.println("数量加一");
            ticketsRec.setNum(1);
            System.out.println(ticketsRec.getNum()+"数量加一");
            ticketsRecService.editTicketsRec(ticketsRec);
        }else{
            System.out.println("新增一条");
            ticketsRecService.postTicketsRec(ticketsRec);
        }
        return CommonResult.success(ticketsRec);

    }

    @DeleteMapping("trec/{id}")
    public CommonResult delTicketsRec(@PathVariable int id){
        int count= ticketsRecService.delTicketsRec(id);
        if(count>0){
            return CommonResult.success();
        }else{
            return CommonResult.fail();
        }
    }

    @PutMapping("trec")
    public CommonResult editTicketsRec(@RequestBody TicketsRec ticketsRec){
        int count=ticketsRecService.editTicketsRec(ticketsRec);
        if(count>0){
            return CommonResult.success(ticketsRec);
        }else{
            return CommonResult.fail();
        }
    }

}
